#include <errno.h>
#include <fstream>
#include <iostream>

const size_t cache_size = 1024;

int main(int argc, char *argv[]) {
  char buf[cache_size];
  std::fstream infile, outfile;
  infile.open("file1.txt", std::ios::in | std::ios::binary);
  if (!infile) {
    strerror_r(errno, buf, cache_size);
    std::cerr << "open file1.txt: " << buf << std::endl;
    return 1;
  }

  outfile.open("file2.txt", std::ios::out | std::ios::binary | std::ios::app);
  if (!outfile) {
    strerror_r(errno, buf, cache_size);
    std::cerr << "open file2.txt: " << buf << std::endl;
    return 1;
  }

  while (!infile.eof()) {
    infile.read(buf, cache_size);
    int n = infile.gcount();
    outfile.write(buf, n);
  }

  infile.close();
  outfile.close();

  std::cout << "append success" << std::endl;

  return 0;
}